package br.ita.comp.ces22.quiz.core.dao.jpa;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import br.ita.comp.ces22.quiz.core.dao.DAO;
import br.ita.comp.ces22.quiz.domain.Question;

public class QuestionJPADAO implements DAO<Question>{
	
	private EntityManager em;
	
	public QuestionJPADAO(){
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("mysql");
		em = emf.createEntityManager();
	}
	public Question getById(Object oIdQuestion){
		Integer idQuestion = (Integer) oIdQuestion;
		Question question = 
		(Question) em.createNamedQuery("Question.findById").setParameter("idQuestion", idQuestion.intValue()).getSingleResult();
		return question;
	}
	public void insert(Question question){
		em.getTransaction().begin();
		em.persist(question);
		em.getTransaction().commit();
	}
}
